/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.contents.transition;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.Timer;

import view.contents.ImageTransitioner;

/**
 * 
 * @author Yang Wang <yang@digitalmenubox.com>
 */
public class BoxOut implements ImageTransition, ActionListener {

	private ImageTransitioner box;
	private BufferedImage bi;
	private Graphics2D g;
	private double accTime;
	private Timer timer;
	private boolean finished;
	private double widthRatio;
	private double heightRatio;

	public BoxOut(ImageTransitioner box) {
		this.box = box;
		this.widthRatio = 0.1;
		this.heightRatio = 0.1;
		bi = new BufferedImage(box.getWidth(), box.getHeight(), BufferedImage.TYPE_INT_ARGB);
		g = (Graphics2D) bi.getGraphics();

		int count = 0;
	}

	@Override
	public void animate(ImageTransitioner box, int time) {
		timer = new Timer(20, this);
		timer.setDelay(20);
		timer.start();
		// make the timer wait until the animation has been finished
		synchronized (timer) {
			try {
				timer.wait();
			} catch (InterruptedException ex) {
				Logger.getLogger(SlideLeft.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
	}

	// public void animate(ImageTransitioner box, int time) {
	// BufferedImage bi = new BufferedImage(box.getWidth(), box.getHeight(),
	// BufferedImage.TYPE_INT_ARGB);
	// Graphics2D g = (Graphics2D) bi.getGraphics();
	// double posRatio = 0;
	// int y = -box.getHeight();
	// double accTime = 0;
	// while (posRatio < 1) {
	// g.setBackground(new Color(255, 255, 255, 0));
	// g.clearRect(0, 0, box.getWidth(), box.getHeight());
	// y = (int)((posRatio-1)*(box.getHeight()));
	// //pass back the transition image to the ImageBox
	// g.drawImage(box.getOverlayImage(), 0, y, null);
	// box.setTransitionImage(bi);
	// posRatio += 0.05;
	// try {
	// Thread.sleep(20);
	// } catch (InterruptedException ex) {
	// Logger.getLogger(SlideDown.class.getName()).log(Level.SEVERE, null, ex);
	// }
	// }
	// }

	public static void main(String[] args) {
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (widthRatio < 1) {
			g.setBackground(new Color(255, 255, 255, 0));
			g.clearRect(0, 0, box.getWidth(), box.getHeight());
			BufferedImage b1 = new BufferedImage((int) (widthRatio * box.getWidth()), (int) (heightRatio * box.getHeight()), BufferedImage.TYPE_INT_ARGB);
			Graphics2D g1 = (Graphics2D) b1.getGraphics();
			AffineTransform xform = AffineTransform.getScaleInstance(widthRatio, heightRatio);
			g1.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
			// g.setRenderingHint(RenderingHints.KEY_RENDERING,
			// RenderingHints.VALUE_RENDER_SPEED);
			g1.drawImage(box.getOverlayImage(), xform, null);
			g.drawImage(b1, (box.getWidth() - (int) (widthRatio * box.getWidth())) / 2, (box.getHeight() - (int) (heightRatio * box.getHeight())) / 2,
					(int) (widthRatio * box.getWidth()), (int) (heightRatio * box.getHeight()), null);
			box.setTransitionImage(bi);
			widthRatio = widthRatio + 0.1;
			heightRatio = heightRatio + 0.1;
		} else {
			synchronized (timer) {
				timer.stop();
				timer.notifyAll();
				this.unsetVariables();
			}
		}
	}

	private void unsetVariables() {
		this.box = null;
		bi = null;
		g = null;
	}
}
